//
//  NavigationView.m
//  自定义导航
//
//  Created by Mac on 16/6/16.
//  Copyright © 2016年 YiXun. All rights reserved.
//

#import "NavigationView.h"
#import "NavigationCell.h"
#import "NavigationOneCell.h"

@interface NavigationView ()<UITableViewDelegate,UITableViewDataSource>
{
    NSString *titleString;
    NSString *titleColor;
}

@property (nonatomic, copy) NSArray *numArr;

@property (nonatomic, strong) UITableView *tableView;


@end
@implementation NavigationView
- (id)initWithFrame:(CGRect)frame title:(NSString *)title colo:(NSString *)colo{
    if (self == [super initWithFrame:frame]) {
        titleString = title;
        titleColor = colo;
        [self showUI];
    }
    return self;
}
#pragma 创建视图初始化
- (void)showUI {
    _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.frame.size.width,self.frame.size.height) style:UITableViewStylePlain];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    _tableView.backgroundColor = [UIColor clearColor];
    _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    _tableView.scrollEnabled = NO;
    [self addSubview:_tableView];
    
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return 1;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 44;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    if ([titleColor isEqualToString:@"2"]) {
        static NSString *cellId = @"NavigationOneCellId";
        NavigationOneCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId];
        if (!cell) {
            //获取xib cell
            cell = [[[NSBundle mainBundle] loadNibNamed:@"NavigationOneCell" owner:self options:nil] lastObject];
        }
        cell.backgroundColor=[UIColor clearColor];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.navTitleLabel.text = titleString;
        cell.navTitleLabel.font = [UIFont systemFontOfSize:16];
        cell.navTitleLabel.textColor = [UIColor blackColor];
        cell.imageV.image = [UIImage imageNamed:@"sj_down"];
        return cell;
    }
    static NSString *cellId = @"NavigationCellId";
    NavigationCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId];
    if (!cell) {
        //获取xib cell
        cell = [[[NSBundle mainBundle] loadNibNamed:@"NavigationCell" owner:self options:nil] lastObject];
    }
    
 
    CGSize size =[titleString sizeWithAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:16]}];
    CGFloat otherWidth = 85 + 1 + 5 + 5 + 12;
    CGFloat width = otherWidth + size.width;
    CGFloat x = (EMM_WIDTH - width)/2;
    
    UIView *middleView = [[UIView alloc] initWithFrame:CGRectMake(x, 0,width, 44)];
    middleView.backgroundColor = [UIColor clearColor];
    [middleView addSubview:cell.titleLabel];
    [cell.contentView addSubview:middleView];
    
    cell.logoImage = [[UIImageView alloc] initWithFrame:CGRectMake(0, 1, 85, 40)];
    [middleView addSubview:cell.logoImage];
    
    cell.hLabel = [[UILabel alloc] initWithFrame:CGRectMake(85, 17, 1, 10)];
    cell.hLabel.backgroundColor = [UIColor whiteColor];
    [middleView addSubview:cell.hLabel];
    
    cell.titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(85 + 1 + 5, 12, size.width, 20)];
    [middleView addSubview:cell.titleLabel];
    
    cell.titleImageView = [[UIImageView alloc] initWithFrame:CGRectMake(85 + 1 + 5 + size.width + 5, 18, 12, 8)];
    [middleView addSubview:cell.titleImageView];
    
    
    cell.backgroundColor=[UIColor clearColor];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.titleLabel.text = titleString;
    cell.titleLabel.font = [UIFont systemFontOfSize:16];
    NSUserDefaults *userdf = [NSUserDefaults standardUserDefaults];
    NSString *logImageUrl = [userdf objectForKey:@"handLogoImg"];
    if ([NSString isStringEmpty:logImageUrl] == NO) {
        cell.logoImage.hidden =NO;
        cell.hLabel.hidden =NO;
        [cell.logoImage sd_setImageWithURL:[NSURL URLWithString:logImageUrl] placeholderImage:nil];
    }else {
        static NSString *cellId = @"NavigationOneCellId";
        NavigationOneCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId];
        if (!cell) {
            //获取xib cell
            cell = [[[NSBundle mainBundle] loadNibNamed:@"NavigationOneCell" owner:self options:nil] lastObject];
        }
        cell.backgroundColor=[UIColor clearColor];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.navTitleLabel.text = titleString;
        cell.navTitleLabel.font = [UIFont systemFontOfSize:16];
        cell.navTitleLabel.textColor = [UIColor whiteColor];
        cell.imageV.image = [UIImage imageNamed:@"pub_jiantou"];
        return cell;
    }
    cell.titleLabel.textColor = [UIColor whiteColor];
    cell.titleImageView.image = [UIImage imageNamed:@"pub_jiantou"];
    return cell;
}

@end
